4. THINK® Pascal and MPW Pascal Porting Issues



This chapter covers issues you will encounter when porting a programming project from Symantec THINK Pascal or Apple's MPW Pascal to the CodeWarrior IDE, including potential problems (and solutions) you might have when porting a project that run on Macintosh computers with Motorola 680x0 processors. This chapter also covers topic to help you port your application to run on PowerPC-based Macintosh computers.

This chapter has these sections:

For related information, see "Mac OS Issues" on page 14.


Pascal Compiler Differences

Notable differences between CodeWarrior Pascal compilers and other Pascal compilers for Mac OS are listed here:


Mac OS Toolbox Initialization

Applications ported from THINK Pascal to CodeWarrior Pascal for Mac OS sometimes crash immediately after they are launched. The reason: THINK Pascal has an option that automatically initializes the Mac OS Toolbox. In CodeWarrior Pascal it is up to you, the programmer, to call the proper initialization routines before using QuickDraw, Window Manager, Menu Manager, and other services.

A typical initialization routine looks like Listing 4.1.


Listing 4.1 An example of initializing the Mac OS Toolbox


(* 
   Initialize the Toolbox before calling any 
   other Mac OS routines.
*)

MaxApplZone; 
MoreMasters;
InitGraf(@qd.thePort);
InitFonts;
InitWindows;
InitMenus;
TEInit;
InitDialogs(NIL);
FlushEvents(everyEvent, 0);
InitCursor;

For more information on Toolbox initialization, refer to Inside Macintosh: Overview.


Pascal Library Differences

This section describes the differences among the libraries and units that CodeWarrior Pascal and other Pascals provide for access to the Mac OS Toolbox.

Most of the topics in these sections cover problems you will encounter when porting older Pascal source code to use the Universal Pascal Interfaces, the interface files published by Apple and distributed with CodeWarrior.

The topics in this section are:


About Universal Interfaces

CodeWarrior Professional always comes with the latest Universal Interfaces, the API for the Mac OS. Apple produces and regularly updates the Universal Interfaces to fix bugs, accommodate new system software features, and anticipate future system software features.

If you are porting a project that previously ran only on 68K to run on PowerPC too, the most notable feature of the Universal Interfaces is the identical access to both the 68K and PowerPC Mac OS platforms. With the Universal Interfaces, the same source code will give identical functionality when compiled for either variety of Macintosh computer.

Many of the differences between other Pascal compilers for Mac OS and CodeWarrior compilers are because of the changes the Universal Interfaces introduce. These changes are described in the following topics.

In Pascal programming for Mac OS, the Universal Interfaces also called the Universal Pascal Interfaces (UPIs).


Using Interfaces and Units

CodeWarrior Pascal requires the explicit inclusion of a USES statement in every source code file that uses the Universal Interfaces or your own units. When moving from other Pascal compilers make sure to include all relevant units.

A USES statement for a typical program might look like this:

  USES Types, QuickDraw, Events, Windows,
   Dialogs, Fonts, DiskInit, TextEdit,
   Traps, Devices, Memory, SegLoad,
   Scrap, ToolUtils, OSUtils, Menus;

One alternative to using this long clause in your source code is to create a unit that uses all the Universal Interfaces your program requires. Include this unit in the USES clause in each of your project's source code files. Then turn on the Uses Propagation option in the Pascal Language settings panel.

Another alternative is to use the precompiled interfaces for 68K and PowerPC, MacOSIntf_68K and MacOSIntf_PPC. Type the name of the appropriate precompiled interface in the Prefix File field of the Pascal Language settings panel.


NOTE:

The OSIntf and ToolIntf units, which were available in MPW Pascal, are not available with the Universal Interfaces.

For more information on Universal Interfaces, see "About Universal Interfaces" on page 27.


QuickDraw Global Variables

The Universal Interfaces require you to access the QuickDraw global variables through a global record variable called qd. Any references made to the Quickdraw globals must use the qd record.

For example, the statement


  InitGraf(@thePort);

becomes


  InitGraf(@qd.thePort);

The QuickDraw global data that must be accessed with the qd qualifier are: randSeed, screenBits, arrow, dkGray, ltGray, gray, black, white, thePort.

For more information on Universal Interfaces, see "About Universal Interfaces" on page 27.


SANE is Obsolete

Consider SANE (Standard Apple Numerics Environment) obsolete when writing or updating software to be used on PowerPC-based Macintosh computers and on 68K-based Macintosh applications running under CFM 68K. SANE is the numerics environment Apple developed for "classic" 68K Macintosh computers.

SANE.p (for Pascal) is no longer part of the Universal Interfaces. Instead, use the routines provided by fp.p.

See also "68K and PowerPC Numerics" on page 29.


68K and PowerPC Numerics

For more information on porting 68K floating point operations and data types to PowerPC, refer to Inside Macintosh: PowerPC Numerics. Appendix A of this manual describes the differences between SANE and PowerPC numerics and offers tips on porting mathematical operations to PowerPC.


extended Data Type

The extended data type is not supported in PowerPC. Instead, use double_t, which is defined for both 68K and PowerPC. Refer to Appendix A of Inside Macintosh: PowerPC Numerics for more information.

For more information on Universal Interfaces, see "About Universal Interfaces" on page 27.

For information on SANE, see "SANE is Obsolete" on page 29.


Procedure Pointers, Callbacks, and UPPs

While ordinary procedure pointers work well when used only with 68K code, Universal Procedure Pointers (UPPs) are required when developing code for both 68K and PowerPC.

Specifically, UPPs allow the Mixed Mode Manager to handle 68K routine calls to PowerPC routines and PowerPC routine calls to 68K routines. UPPs are versatile, allowing you to combine PowerPC and 68K object code in the same program without dealing with mode switches, parameter order on the stack, and other low-level issues. On the downside, converting source code to the PowerPC means explicitly creating a UPP every time you need to provide a callback routine to a Mac OS routine or any other piece of software that may either be in 68K or PowerPC object code.

Ways to create a new UPP are:

To destroy a UPP:

  • call the DisposeRoutineDescriptor routine (in MixedMode.p)
  • For example, to create a new ResErrUPP in Pascal that you can pass to the Resource Manager you would call:


      FUNCTION NewResErrProc(    userRoutine: ResErrProcPtr): ResErrUPP;

    passing the name of your procedure as the single parameter. NewResErrProc and other UPP-creating routines are implemented as glue code that is linked to your program automatically.

    The Universal Interfaces provide routines to directly call universal procedure pointers. These routines are the interface to the Mixed Mode Manager.

    A typical calling function might look like this:


      PROCEDURE CallResErrProc(thErr: OSErr;    userRoutine: ResErrUPP);

    When you are done with this UPP, dispose of it by calling


      DisposeRoutineDescriptor(userRoutine);

    These calls are only needed for writing a callback to a Mac OS routine that exists in a different architecture (PowerPC calling 68K or 68K calling PowerPC).

    Note that these calling functions are only needed when you write code that expects a callback parameter from an outside source. Generally this is only required if your program allows loadable sub-modules or patches traps that expect callback functions. Thus you would only have to use the CallResErrProc procedure if you install a patch to the Resource Manager that needs to use the ResError callback routine.

    For more information on Universal Interfaces, see "About Universal Interfaces" on page 27.





    Visit the Metrowerks website at: http://www.metrowerks.com
    For assistance contact Metrowerks Technical Support at: support@metrowerks.com
    Copyright © 1999, Metrowerks Corp. All rights reserved.

    Last updated: March 01, 1999 * Chris Magnuson * John Roseborough